package com.mflex.mwms.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Base64;
import android.util.Xml;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.mflex.mwms.R;
import com.mflex.mwms.adapter.HuaweiCompareAdapter;
import com.mflex.mwms.adapter.StockTaskDetailAdapter;
import com.mflex.mwms.model.ASNInfo;
import com.mflex.mwms.model.ASNboxNum;
import com.mflex.mwms.model.CustomApiResult;
import com.mflex.mwms.model.HuaweiSNInfo;
import com.mflex.mwms.model.Inventoryproduct;
import com.mflex.mwms.model.SaveInfo;
import com.mflex.mwms.utils.MMKVUtils;
import com.mflex.mwms.utils.MessageUtils;
import com.mflex.mwms.utils.XToastUtils;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.CallBackProxy;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.Utils;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.net.JSONUtils;
import com.xuexiang.xutil.net.JsonUtil;

import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HuaweiSnCompareActivity extends BaseActivity {
    @BindView(R.id.txtASNNo)
    EditText txtASNNo;

    @BindView(R.id.txtBOXID)
    EditText txtBOXID;

    @BindView(R.id.txtCustomPN)
    EditText txtCustomPN;

    @BindView(R.id.txtPN)
    EditText txtPN;

    @BindView(R.id.txtQty)
    EditText txtQty;

    @BindView(R.id.txtRev)
    EditText txtRev;

    @BindView(R.id.txtHuaweiSN)
    EditText txtHuaweiSN;

    @BindView(R.id.txt09Code)
    EditText txt09Code;

    @BindView(R.id.txtDateCode)
    EditText txtDateCode;

    @BindView(R.id.txtBoxNum)
    EditText txtBoxNum;

    @BindView(R.id.button_clear)
    Button buttonClear;

    MiniLoadingDialog mMiniLoadingDialog;

    private String jsonStr;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_huawei_sn_compare);

        ButterKnife.bind(this);
        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this, "加载...");

        txtASNNo.setOnKeyListener(this);
        txtBOXID.setOnKeyListener(this);
        txtHuaweiSN.setOnKeyListener(this);
        txt09Code.setOnKeyListener(this);

        txtASNNo.setSelectAllOnFocus(true);
        txtBOXID.setSelectAllOnFocus(true);
        txtHuaweiSN.setSelectAllOnFocus(true);
        txt09Code.setSelectAllOnFocus(true);

        txtASNNo.requestFocus();

        buttonClear.setOnClickListener(this);

        jsonStr = "";
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button_clear:
                clearButtonMd(view);
                break;

            default:
                break;
        }
    }

    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && (i == 66 || i == 61)) {
            switch (view.getId()) {
                case R.id.txtASNNo:
                    checkASNNo(view);
                    break;
                case R.id.txtBOXID:
                    checkBoxId(view);
                    break;
                case R.id.txtHuaweiSN:
                    checkHuaweiSN(view);
                    break;
                case R.id.txt09Code:
                    check09Code(view);
                    break;
                default:
                    break;
            }
        }
        return false;
    }

    private void checkASNNo(View v) {
        if (StringUtils.isEmpty(txtASNNo.getText().toString())) {
            txtASNNo.postDelayed(new Runnable() {
                @Override
                public void run() {
                    txtASNNo.requestFocus();
                }
            },200);
            return;
        }

        mMiniLoadingDialog.show();
        XHttp.get("api/Comparison/ASN_GetBoxNum")
                .params("ASNNo", txtASNNo.getText().toString())
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(120000)
                .execute(new CallBackProxy<CustomApiResult<ASNboxNum>, ASNboxNum>(new SimpleCallBack<ASNboxNum>() {

                    @Override
                    public void onSuccess(ASNboxNum response) {
                        mMiniLoadingDialog.dismiss();
                        txtBoxNum.setText(String.valueOf(response.getBoxNum()));
                        txtBOXID.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                txtBOXID.requestFocus();
                            }
                        },200);
                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
                        txtASNNo.setText("");
                        txtASNNo.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                txtASNNo.requestFocus();
                            }
                        },200);
                        MessageUtils.errorMsgAndVoice(HuaweiSnCompareActivity.this, e);
                    }
                }) {
                });
    }

    private void checkBoxId(View v) {
        if (StringUtils.isEmpty(txtASNNo.getText().toString())) {
            txtASNNo.postDelayed(new Runnable() {
                @Override
                public void run() {
                    txtASNNo.requestFocus();
                }
            },200);
            MessageUtils.errorMsgAndVoice(HuaweiSnCompareActivity.this, "未扫描ASN号");
            return;
        }
        if (StringUtils.isEmpty(txtBOXID.getText().toString())) {
            txtBOXID.postDelayed(new Runnable() {
                @Override
                public void run() {
                    txtBOXID.requestFocus();
                }
            },200);
            return;
        }

        mMiniLoadingDialog.show();
        XHttp.get("api/Comparison/ASN_GetBoxId")
                .params("BoxID", txtBOXID.getText().toString())
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(120000)
                .execute(new CallBackProxy<CustomApiResult<ASNInfo>, ASNInfo>(new SimpleCallBack<ASNInfo>() {

                    @Override
                    public void onSuccess(ASNInfo response) {
                        mMiniLoadingDialog.dismiss();
                        txtCustomPN.setText(response.getCustomPN());
                        txtPN.setText(response.getPN());
                        txtQty.setText(String.valueOf(response.getQty()));
                        txtRev.setText(response.getRev());
                        txtDateCode.setText(response.getDateCode());
                        txtHuaweiSN.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                txtHuaweiSN.requestFocus();
                            }
                        },200);
                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
                        txtBOXID.setText("");
                        txtBOXID.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                txtBOXID.requestFocus();
                            }
                        },200);
                        MessageUtils.errorMsgAndVoice(HuaweiSnCompareActivity.this, e);
                    }
                }) {
                });
    }

    private void checkHuaweiSN(View v) {
        if (StringUtils.isEmpty(txtASNNo.getText().toString())) {
            txtASNNo.postDelayed(new Runnable() {
                @Override
                public void run() {
                    txtASNNo.requestFocus();
                }
            },200);
            MessageUtils.errorMsgAndVoice(HuaweiSnCompareActivity.this, "未扫描ASN号");
            return;
        }
        if (StringUtils.isEmpty(txtBOXID.getText().toString())) {
            txtBOXID.postDelayed(new Runnable() {
                @Override
                public void run() {
                    txtBOXID.requestFocus();
                }
            },200);
            MessageUtils.errorMsgAndVoice(HuaweiSnCompareActivity.this, "未扫描箱号");
            return;
        }
        if (StringUtils.isEmpty(txtHuaweiSN.getText().toString())) {
            txtHuaweiSN.postDelayed(new Runnable() {
                @Override
                public void run() {
                    txtHuaweiSN.requestFocus();
                }
            },200);
            return;
        }
        String huaweiSN = txtHuaweiSN.getText().toString();
        byte[] huaweiSNByte = txtHuaweiSN.getText().toString().getBytes();


        Gson gson = new Gson();
        jsonStr = gson.toJson(huaweiSNByte);
        mMiniLoadingDialog.show();
        XHttp.get("api/Comparison/ASN_GetHuaweiSN")
                .params("HuaweiSN", jsonStr)
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(120000)
                .execute(new CallBackProxy<CustomApiResult<HuaweiSNInfo>, HuaweiSNInfo>(new SimpleCallBack<HuaweiSNInfo>() {

                    @Override
                    public void onSuccess(HuaweiSNInfo response) {
                        mMiniLoadingDialog.dismiss();

                        String dc = response.getDateCode();
                        String qty = response.getQty();
                        String rev = response.getRev();

                        if(!StringUtils.equals(dc,txtDateCode.getText().toString())){
                            MessageUtils.errorMsgAndVoice(HuaweiSnCompareActivity.this, "华为SN的DateCode[" + dc + "]与箱号DateCode不匹配");
                            txtHuaweiSN.setText("");
                            txtHuaweiSN.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    txtHuaweiSN.requestFocus();
                                }
                            },200);
                            return;
                        }

                        if(!StringUtils.equals(qty,txtQty.getText().toString())){
                            MessageUtils.errorMsgAndVoice(HuaweiSnCompareActivity.this, "华为SN的Qty[" + qty + "]与箱号Qty不匹配");
                            txtHuaweiSN.setText("");
                            txtHuaweiSN.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    txtHuaweiSN.requestFocus();
                                }
                            },200);
                            return;
                        }

                        if(!StringUtils.equals(rev,txtRev.getText().toString())){
                            MessageUtils.errorMsgAndVoice(HuaweiSnCompareActivity.this, "华为SN的Rev[" + rev + "]与箱号Rev不匹配");
                            txtHuaweiSN.setText("");
                            txtHuaweiSN.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    txtHuaweiSN.requestFocus();
                                }
                            },200);
                            return;
                        }

                        txt09Code.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                txt09Code.requestFocus();
                            }
                        },200);
                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
                        txtHuaweiSN.setText("");
                        txtHuaweiSN.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                txtHuaweiSN.requestFocus();
                            }
                        },200);
                        MessageUtils.errorMsgAndVoice(HuaweiSnCompareActivity.this, e);
                    }
                }) {
                });
    }

    private void check09Code(View v) {
        if (StringUtils.isEmpty(txtASNNo.getText().toString())) {
            txtASNNo.postDelayed(new Runnable() {
                @Override
                public void run() {
                    txtASNNo.requestFocus();
                }
            },200);
            MessageUtils.errorMsgAndVoice(HuaweiSnCompareActivity.this, "未扫描ASN号");
            return;
        }
        if (StringUtils.isEmpty(txtBOXID.getText().toString())) {
            txtBOXID.postDelayed(new Runnable() {
                @Override
                public void run() {
                    txtBOXID.requestFocus();
                }
            },200);
            MessageUtils.errorMsgAndVoice(HuaweiSnCompareActivity.this, "未扫描箱号");
            return;
        }
        if (StringUtils.isEmpty(txt09Code.getText().toString())) {
            txt09Code.postDelayed(new Runnable() {
                @Override
                public void run() {
                    txt09Code.requestFocus();
                }
            },200);
            MessageUtils.errorMsgAndVoice(HuaweiSnCompareActivity.this, "未扫描华为SN号");
            return;
        }
        if (StringUtils.isEmpty(txt09Code.getText().toString())) {
            txt09Code.postDelayed(new Runnable() {
                @Override
                public void run() {
                    txt09Code.requestFocus();
                }
            },200);
            return;
        }

        SaveInfo saveInfo = new SaveInfo();
        saveInfo.setASNNo(txtASNNo.getText().toString());
        saveInfo.setBoxId(txtBOXID.getText().toString());
        saveInfo.setHuaweiSN(jsonStr);
        saveInfo.set_09Code(txt09Code.getText().toString());

        mMiniLoadingDialog.show();
        XHttp.post("api/Comparison/ASN_SaveHuaWei")
                .upJson(JsonUtil.toJson(saveInfo))
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(120000)
                .execute(new CallBackProxy<CustomApiResult<String>, String>(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String response) {
                        mMiniLoadingDialog.dismiss();
                        initPage();
                        XToastUtils.success("校验成功，已保存");
                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
                        txt09Code.setText("");
                        txt09Code.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                txt09Code.requestFocus();
                            }
                        },200);
                        MessageUtils.errorMsgAndVoice(HuaweiSnCompareActivity.this, e);
                    }
                }) {
                });
    }

    private void clearButtonMd(View view){
        initPage();
        XToastUtils.success("重置成功");
    }

    private void initPage(){
        txtASNNo.setText("");
        txtBOXID.setText("");
        txtCustomPN.setText("");
        txtPN.setText("");
        txtQty.setText("");
        txtRev.setText("");
        txtHuaweiSN.setText("");
        txt09Code.setText("");
        txtDateCode.setText("");
        txtBoxNum.setText("");
        jsonStr = "";
        txtASNNo.postDelayed(new Runnable() {
            @Override
            public void run() {
                txtASNNo.requestFocus();
            }
        },200);
    }
}